<?php
/**
 * Returns the URL of the currently active template
 * 
 * @package Concerto
 * @subpackage Template Functions
 * @return string The template URL
 */
function templateUrl() {
	$template = getOption('template');
	$template = Modulator()->runFilter('template_url', CN_ROOT_URL . 'templates/'.$template.'/');
	return $template;
}

/**
 * Returns the filesystem path of the currently active template
 * 
 * @package Concerto
 * @subpackage Template Functions
 * @return string The template path
 */
function templateDir() {
	$template = getOption('template');
	$template = Modulator()->runFilter('template_dir', CN_DOCUMENT_ROOT . 'templates/'.$template.'/');
	return $template;
}

/**
 * Runs system functions for the template header
 *
 * * Includes jQuery
 * * Runs the template_header Modulator function
 * * Runs the displayHeader() function on any fields attached to the current content type
 * * Initializes the concerto javascript classes
 * 
 * @package Concerto
 * @subpackage Template Functions
 * @see \application\datatypes\displayHeader()
 */
function cnHeader() {
	Modulator()->runFunction('template_header');
	Modulator()->includeScript('jquery');
	// Include all javascript for fields attached to content type
	if($type = getQueryVar('type')) {
		$fields = DB()->getResults("SELECT * FROM `cn_content_fields` WHERE `cf_content_type`='$type'");
		foreach($fields as $field) {
			$class = 'application\\datatypes\\' . $field->cf_data_type;
			$class::displayHeader($field);
		}
	}
	?>
	<script type="text/javascript" src="<?=getCommonFile('concerto/javascript/concerto.js')?>"></script>
	<script type="text/javascript">
		concerto.go('<?=CN_URL?>');
	</script>
	<?php
}

/**
 * Displays breadcrumbs, based on information gathered from the URL
 *
 * @param string $separator Text to display between each breadcrumb element
 * @package Concerto
 * @subpackage Template Functions
 */
function breadcrumbs($separator = '') {
	global $concerto;
	$urlInfo = $concerto->UrlInterpreter->urlInfo;
	$i = 0;
	$count = count($urlInfo);
	?>
	<ul class="breadcrumbs">
		<li class="breadcrumbs-home"><a href="<?=CN_URL?>">Home</a><span class="separator"><?=$separator?></span></li>
		<?php foreach($urlInfo as $part) : $i++;?>
		<?php if($part['resource'] == 'tag_group') continue; ?>
		<?php if($i == $count) : ?>
		<li class="breadcrumbs-last-level"><?=getTitle($part['resource'], $part['id'])?></li>
		<?php else : ?>
		<li class="breadcrumbs-level-<?=$i?>"><a href="<?=getPermalink($part['resource'], $part['id'])?>"><?=getTitle($part['resource'], $part['id'])?></a><span class="separator"><?=$separator?></span></li>
		<?php endif;?>
			
		<?php endforeach;?>
		
	</ul>
	<?php
}

/**
 * Simple check if the current user is logged in
 *
 * @return bool TRUE or FALSE
 * @package Concerto
 * @subpackage Template Functions
 */
function loggedIn() {
	global $concerto;
	if(!$concerto->User) return FALSE;
	else return $concerto->User->getData('user_id');
}

/**
 * Simple check if the current user is a global administrator
 *
 * @return bool TRUE or FALSE
 * @package Concerto
 * @subpackage Template Functions
 */
function isAdmin() {
	global $concerto;
	if(!$concerto->User) return FALSE;
	else return ($concerto->User->getData('user_level') > 1);
}

/**
 * Simple check if the current page is the home page
 *
 * @return bool TRUE or FALSE
 * @package Concerto
 * @subpackage Template Functions
 */
function isHome() {
	global $concerto;
	$currentUrl = $concerto->UrlInterpreter->currentUrl;
	
	if($currentUrl == CN_URL) return TRUE;
	return FALSE;
}

/**
 *

/**
 * Displays a navigation menu
 *
 * Displays a navigation menu as defined in the admin panel for nav menus.
 *
 * @param string $menu The menu to display. Exact same name as the navigation menu in the admin panel
 * @param bool $links Whether or not to link elements in the menu to their respective URLs
 * @param bool $home_link Whether or not to generate a link to the home page as the first element in the list
 * @param string $before Text to display before each element
 * @param string $after Text to display after each element
 * @package Concerto
 * @subpackage Template Functions
 * @see \application\models\NavMenu
 */
function navMenu($menu, $links = TRUE, $homeLink = FALSE, $li = TRUE, $before = '', $after = '', $separator = '') {
	$instance = new \application\models\NavMenu;
	$instance->navigationMenu($menu, $links, $homeLink, $li, $before, $after, $separator);
}

/**
 * @deprecated
 */
function displayBox($box) {
	echo DB()->getVar("SELECT `box_content` FROM `cn_boxes` WHERE `box_name`='$box'");
}

/**
 * Display all header information for the current form
 *
 * @see \application\models\Form::formHeader()
 * @package Concerto
 * @subpackage Template Functions
 */
function formHeader() {
	global $concerto;
	$concerto->view->formHeader();
}


/**
 * Displays a form field on a custom form template
 *
 * @see \application\models\Form::displayField()
 * @param string $field The name of the field to display
 * @package Concerto
 * @subpackage Template Functions
 */
function displayFormField($field) {
	global $concerto;
	$concerto->view->displayFormField($field);
}

/**
 * Displays messages for the form
 *
 * @see \application\models\Form::displayMessages()
 * @package Concerto
 * @subpackage Template Functions
 */
function displayFormMessages() {
	global $concerto;
	$concerto->view->displayFormMessages();
}

/**
 * Displays the opening form tags and any hidden fields for a form
 *
 * @see \application\models\Form::openForm()
 * @package Concerto
 * @subpackage Template Functions
 */
function openForm() {
	global $concerto;
	$concerto->view->openForm();
}

/**
 * Displays the closing form tags
 *
 * @see \application\models\Form::closeForm()
 * @package Concerto
 * @subpackage Template Functions
 */
function closeForm() {
	global $concerto;
	$concerto->view->closeForm();
}

/**
 * Displays the submit button for the form
 *
 * @see \application\models\Form::submitButton()
 * @param string $text The text for the button
 * @param string $class Optional class to apply to the button
 * @package Concerto
 * @subpackage Template Functions
 */
function submitButton($text, $class = null) {
	global $concerto;
	$concerto->view->submitButton($text, $class);
}

/**
 * Display the default form layout
 *
 * @see \application\models\Form::defaultLayout()
 * @package Concerto
 * @subpackage Template Functions
 */
function defaultLayout() {
	global $concerto;
	$concerto->view->defaultLayout();
}


function formSuccess() {
	global $concerto;
	return $concerto->view->formSuccess();
}

/**
 * Display the display name of the current user
 * 
 * Looks for username, and if not set, gets email
 *
 * @param int $id Optional id for the user; if not provided, uses the current user
 * @package Concerto
 * @subpackage Template Functions
 */
function displayName($id = null) {
	if(!$id) { // Get current user display name
		global $concerto;
		return $concerto->User->displayName();
	}
	else {
		$user = new \application\models\User($id);
		return $user->displayName();
	}	
}





?>